/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.activities;

import java.util.List;

import com.weissenborn.colabtex.client.ClientFactory;
import com.weissenborn.colabtex.client.places.Workplace;
import com.weissenborn.colabtex.client.service.LaTexService;
import com.weissenborn.colabtex.client.service.LaTexServiceAsync;
import com.weissenborn.colabtex.client.view.TexView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

// TODO: Auto-generated Javadoc
/**
 * The Class TexActivity.
 */
public class TexActivity extends AbstractActivity implements TexView.Presenter {

	/** The client factory. */
	private ClientFactory clientFactory;
	
	/** The place. */
	private Workplace place;
	
	/** The view. */
	private TexView view;
	
	/** The popup. */
	private final PopupPanel popup = new PopupPanel(false);
	
	/**
	 * Instantiates a new tex activity.
	 *
	 * @param place the place
	 * @param clientFactory the client factory
	 */
	public TexActivity(Workplace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.place = place;		
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getTexView();		
		view.setPresenter(this);
		
		view.setName(place.getName());
		view.setUrl(place.getPadUrl());
		
		Cookies.setCookie("padId", place.getPadUrl().substring(place.getPadUrl().lastIndexOf("/")+1));    
		
		containerWidget.setWidget(view.asWidget());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#goTo(com.google.gwt.place.shared.Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#compilePdf()
	 */
	public void compilePdf() {
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onSuccess(String result) {
				Window.alert(result);
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");				
			}
		};		
		service.createOrUpdatePdf(place.getPadUrl().substring(place.getPadUrl().lastIndexOf("/")+1), callback);
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.AbstractActivity#mayStop()
	 */
	public String mayStop() {
		Cookies.removeCookie("padId");
		return super.mayStop();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#getLog()
	 */
	public void getLog(final TextArea area) {
		String padId = Cookies.getCookie("padId");
		
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			
			public void onSuccess(String result) {				
				area.setText(result);
			}
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		};
		
		service.getCompileLog(padId, callback);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#sendTexFile()
	 */
	public void sendTexFile() {
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {			
			public void onSuccess(Boolean result) {
				if(result)
					Window.alert("File sent!");
				else
					Window.alert("Couldn't send file!");				
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");				
			}
		};
		
		service.sendTexFile(Cookies.getCookie("padId"), clientFactory.getUser().getId(), callback);
	}


	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#sendPdfFile()
	 */
	public void sendPdfFile() {
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {			
			public void onSuccess(Boolean result) {
				if(result)
					Window.alert("File sent!");
				else
					Window.alert("Couldn't send file!");				
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");				
			}
		};
		
		service.sendPdfFile(Cookies.getCookie("padId"), clientFactory.getUser().getId(), callback);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#deleteResource(java.lang.String)
	 */
	public void deleteResource(final String filename) {
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {			
			public void onSuccess(Boolean result) {
				if(result) {
					Window.alert("Deleted Resource");
					view.deleteResource(filename);
				}
				else
					Window.alert("Couldn't send file!");				
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");				
			}
		};
		
		service.deleteResource(filename, Cookies.getCookie("padId"), callback);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.TexView.Presenter#getResources()
	 */
	public void getResources() {
		LaTexServiceAsync service = LaTexService.Util.getInstance();
		
		AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {			
			public void onSuccess(List<String> result) {					
				view.setResources(result);			
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");				
			}
		};
		
		service.getResources(Cookies.getCookie("padId"), callback);
	}
}
